<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'/>
<link href='do.css' rel='stylesheet' type='text/css'/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript">

function formatXml(xml) {
	var formatted = '';
	var reg = /(>)(<)(\/*)/g;
	xml = xml.replace(reg, '$1\r\n$2$3');
	var pad = 0;
	jQuery.each(xml.split('\r\n'), function(index, node) {
	    var indent = 0;
	    if (node.match( /.+<\/\w[^>]*>$/ )) {
	        indent = 0;
	    } else if (node.match( /^<\/\w/ )) {
	        if (pad != 0) {
	            pad -= 1;
	        }
	    } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
	        indent = 1;
	    } else {
	        indent = 0;
	    }
	
	    var padding = '';
	    for (var i = 0; i < pad; i++) {
	        padding += '  ';
	    }
	
	    formatted += padding + node + '\r\n';
	    pad += indent;
	});
	
	return formatted;
}
	
$(document).on({
    ajaxStart: function() {
        $(this.body).addClass("loading");
    },
    ajaxStop: function() {
        $(this.body).removeClass("loading");
    }
});

//jQuery.support.cors = true;

$(document).ready(function(){
  $("button#processBtn").click(function(){
  	
  	reqData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\<s:Body><batchRequest xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:oasis:names:tc:DSML:2:0:core\"\ requestID=\"Batch of search requests\" responseOrder=\"Unordered\">"
					 + jQuery("textarea#dsmlReq").val()
					 + "</batchRequest></s:Body></s:Envelope>";
					 
    $.ajax({
	    url: 'LdConnection.svc'
	    ,type: 'POST'
	    ,async: true
	    ,data: reqData
	    ,headers: {
	        //"Accept-Encoding": 'gzip, deflate',
	        //"Content-Type": 'text/xml; charset=utf-8',
	        SOAPAction: "\"Process\""
	    }
	    ,contentType: 'text/xml; charset=utf-8'
	    ,dataType: 'xml'
	    ,timeout: 90000
	    ,success: function(xmlData, status, xhr)
	    {
	        //console.info(xmlData);
	        //alert("Data: " + xmlData.toString());
	        jQuery("textarea#dsmlRes").val(formatXml(xhr.responseText))
	        //jQuery("#output").text(formatXml(xhr.responseText))
	        //alert(xhr.responseText);
	        //jQuery("#output").innerHtml = "<test><g>asda</g><g>adfsdf</g></test>"
	        //alert(xhr.responseXML);
	        //var data = $(xmlData).find('batchResponse').text();
	        //jQuery("#output").html(data);
	        //prettyPrint();
	    }
	    ,error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
		});
		return false;
  });
  $("button#addBtn").click(function(){
		var operation = "";
  	
		switch($("#addCmdCbo").val()){
			case "search":
				operation =	"<searchRequest	dn=\"{@replaceDN}\" requestID=\"some search...\" scope=\"wholeSubtree\" derefAliases=\"neverDerefAliases\" typesOnly=\"false\"><filter><and><present name=\"objectClass\"/><equalityMatch name=\"objectClass\"><value>data acl</value></equalityMatch></and></filter><attributes></attributes></searchRequest>"
				break;
				
			case "modify":
				operation =	"<modifyRequest dn=\"{@replaceDN}\"><modification name=\"description\" operation=\"replace\"><value>This is the new description</value></modification><modification name=\"description\" operation=\"add\"><value>This is the new description</value></modification><modification name=\"description\" operation=\"delete\"><value>This is the new description</value></modification></modifyRequest>"
				break;
				
			case "add":
				operation =	"<addRequest dn=\"{@replaceDN}\"><attr name=\"objectClass\"><value>top</value><value>person</value></attr><attr name=\"uid\"><value>test.user</value></attr></addRequest>"
				break;
				
			case "modifyDn":
				operation =	"<modDNRequest dn=\"{@replaceDN}\" newrdn=\"fill new value here...\" deleteoldrdn=\"true\" newSuperior=\"fill new value here...\" />"
				break;
				
			case "delete":
				operation =	"<delRequest dn=\"{@replaceDN}\"/>"
				break;
				
			case "compare":
				operation =	"<compareRequest dn=\"{@replaceDN}\"> <assertion name=\"attrib name...\"><value>some value...</value></assertion></compareRequest>"
				break;
				
		}
		
		operation = operation.replace("{@replaceDN}", $("#dnTxt").val());
		
		$("textarea#dsmlReq").val(
					$("textarea#dsmlReq").val()
					+ formatXml(operation)
				);
    
		return false;
  });
  
  $("button#addUuidBtn").click(function(){
  	var data = "";
  	
  	$.ajax({
	    //url: '../../../../../public/app/uhul/PRX/CD/www.fileformat.info/tool/guid.htm?count=1&format=text&hyphen=y&uppercase=y'
	    url: 'https://www.fileformat.info/tool/guid.htm?count=1&format=text&hyphen=y&uppercase=y'
	    ,type: 'get'
	    ,async: true
	    ,dataType: 'text'
	    ,timeout: 5000
	    ,success: function(xmlData, status, xhr)
	    {
	    	data = "cn=" + xhr.responseText + ",";
	      $("#dnTxt").val(
	      	data + $("#dnTxt").val()
				);
	    }
	    ,error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
		});
  
		return false;
  });
  
});
</script>
</head>
<body>
<form>
	<p style="margin:5px">Send DSML v2.0 request to UHUL DSML gate</p>
	<!--<p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</p>
	<p>&lt;s:Envelope xmlns:s=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; &gt;</p>
	<p>  &lt;s:Body&gt;</p>
	<p>    &lt;batchRequest</p>
	<p>      xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;</p>
	<p>      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</p>
	<p>      xmlns=&quot;urn:oasis:names:tc:DSML:2:0:core&quot;</p>
	<p>      requestID=&quot;Batch of requests&quot;</p>
	<p>      responseOrder=&quot;Unordered&quot;</p>
	<p>    &gt;</p>-->
	<div id="splitArea">
		<div id="top">
			<div id="toolbar">
				<span> add command:</span>
				<select id="addCmdCbo">
					<option value="search">search</option>
					<option value="compare">compare</option>
					<option value="add">add</option>
					<option value="delete">delete</option>
					<!--<option value="delete tree">delete tree</option>-->
					<option value="modify">modify</option>
					<option value="modifyDn">modifyDn</option>
				</select>
				<span> for </span>
				<button id="addUuidBtn">newId</button>
				<span> DN: </span>
				<input id="dnTxt" type="text" value="cn=">
				<button id="addBtn">Add</button>
				<button id="processBtn">Process</button>
			</div>
			<div id="topContent"><textarea id="dsmlReq"></textarea></div>
		</div>
		<!--<p>    &lt;/batchRequest&gt;</p>
		<p>  &lt;/s:Body&gt;</p>
		<p>&lt;/s:Envelope&gt;</p>-->
		<div id="bottom">
			<textarea id="dsmlRes"></textarea>
			<!--<img id="spinner" src="big-spinner.gif" title="" alt=""/>-->
		</div>
	</div>
</form>
<div class="modal"></div>
</body>
</html>